package com.spring.common.annotation;

import com.spring.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

@Component
public class StuCodeValidator implements ConstraintValidator<StuCodeValid,String> {

    @Autowired
    private StudentService studentService;
    /**
     * 参数有效性校验
     * @param value
     * @param context
     * 校验规则:
     * 如果学生学号发生重复为无效返回false;
     * 如果学生学号不重复会有效,则返回true;
     * @return
     */
    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        if (value==null) {
            return false;
        }
        //查询学号是否重复,如果重复返回true,否则近回false;
        boolean flag = studentService.queryStuCodeRepeat(value);
        return !flag;
    }
}
